package com.szeldon.pj.jps.exp;

import com.szeldon.pj.jps.data.store.SBAStore;
import com.szeldon.pj.jps.datastore.SimpleObject;
import com.szeldon.pj.jps.envs.ENVS;
import com.szeldon.pj.jps.qres.AbstractQueryResult;
import com.szeldon.pj.jps.qres.BagResult;
import com.szeldon.pj.jps.qres.BooleanResult;
import com.szeldon.pj.jps.qres.QResStack;
import com.szeldon.pj.jps.visitor.ASTVisitor;

public class EqualsBinaryExpression extends BinaryExpression {

	/**
	 * 
	 * @param leftExpression
	 * @param rightExpression
	 */
	public EqualsBinaryExpression(Expression leftExpression, Expression rightExpression) {
		super(leftExpression, rightExpression);
	}

	/**
	 * 
	 * @param visitor
	 * @param envs
	 * @param qResStack
	 * @param store
	 */
	public void accept(ASTVisitor visitor, ENVS envs, QResStack qResStack, SBAStore store) {

		if (!(leftExpression instanceof NameExpression)) {
			throw new IllegalStateException("Equals expression must have NameExpression as a left expression. "
					+ " Currently it's " + leftExpression.getClass().getName() + ".");
		}

		BagResult leftResultBag = envs.bind(((NameExpression) leftExpression).getName());
		AbstractQueryResult leftResult = leftResultBag.getElements().iterator().next();

		qResStack.push(new BooleanResult(ASTVisitor.compareAbstractQueryResultWithExpression(store, leftResult,
				(TerminalExpression) rightExpression) == 0));
	}

	@Override
	public int compareTo(SimpleObject object) throws IllegalStateException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getValueAsString() {
		return "==";
	}
}
